<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <link rel="shortcut icon" href="../static/favicon.ico"/>
    <link rel="bookmark" href="/favicon.ico"/>
    <link rel="stylesheet" type="text/css" href="../static/css/base.css">
    <link rel="stylesheet" type="text/css" href="../static/css/register.css">
</head>

<body>
    <div class="wrapper">
        <div class="head">
            <ul>
                <li><a href="/index"><img src="../static/img/logo.png" alt="logo"></a></li>
                <li class="divider"></li>
                <li>注册</li>
            </ul>
        </div>
    </div>

    <div class="content">
        <form action="/userRegister" method="post">
            <p class="pass-form-item">
                <label class="pass-label">用户名</label>
                <input id="username" type="text" name="username" class="pass-text-input" placeholder="请设置用户名">
            </p>
            <p class="pass-form-item">
                <label class="pass-label">手机号</label>
                <input id="phone" type="text" name="phone" class="pass-text-input" placeholder="可用于找回密码">
            </p>
            <p class="pass-form-item">
                <label class="pass-label">密码</label>
                <input id="password" type="password" name="password" class="pass-text-input" placeholder="请设置登录密码">
            </p>
            <p class="pass-form-item">
                <label class="pass-label">确认密码</label>
                <input id="repassword" type="password" name="Repassword" class="pass-text-input" placeholder="请确认登录密码">
            </p>
            <div class="login-link">
                <span>我已注册，现在就</span>
                <a href="/login">登录</a>
            </div>
            <div class="clearfix"></div>
            <p class="pass-form-item">
                <input type="submit" value="注册" class="pass-button">
            </p>
        </form>
    </div>

    <script type="text/javascript" src="../static/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="../static/js/jquery-form.min.js"></script>
    <script type="text/javascript" src="../static/js/base.js"></script>
    <script>
        $("input").blur(function () {
            sessionStorage.setItem("tabLiNum",0);
            var $parent = $(this).parent();
            $parent.find(".formtips").remove();
            if($(this).is("#username")){
                if (this.value=="") {//为空
                    $parent.append("<div class='formtips'>用户名不能为空！</div>")
                }
            }
            if($(this).is("#password")){
                if (this.value=="") {//为空
                    $parent.append("<div class='formtips'>密码不能为空！</div>")
                }else{
                    if(this.value.length<6){
                        $parent.append("<div class='formtips'>密码长度不能小于6位！</div>")
                    }
                }
            }
            if($(this).is("#phone")){
                if (this.value=="") {//为空
                    $parent.append("<div class='formtips'>手机号不能为空！</div>")
                }else{
                    if(this.value.length!=11){
                        $parent.append("<div class='formtips'>请输入11位有效手机号！</div>")
                    }
                }
            }
            if($(this).is("#repassword")){
                if (this.value=="") {//为空
                    $parent.append("<div class='formtips'>确认密码不能为空！</div>")
                }else{
                    if(this.value!=$("#password").val()){
                        $parent.append("<div class='formtips'>两次密码不一致！</div>")
                    }
                }
            }
        })
        $("form").submit(function () {
            $("form input").trigger("blur");//执行之后会增加<div>元素
            if($(".formtips").length>0) return false;
        })
        $("form").ajaxForm(function(data){
            if(data=="success"){
                alert("注册成功！");
                window.location.href="/login";
            }else{
                alert("用户名已存在！")
            }
        });
    </script>
</body>

</html>